<template>
  <div class="dashboard-container">
    房间类型管理

    <el-dialog
      title="新增"
      :visible.sync="addButtonController"
      width="30%"
      @close="closeRoomType('ruleForm')"
    >
      <el-form label-width="20%" :model="addList" ref="ruleForm">
        <!--        <el-form-item label="房间类型表ID" prop="roomTypeTableId">-->
        <!--          <el-input v-model="addList.roomTypeTableId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型表ID"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="房间类型ID" prop="roomTypeId">-->
        <!--          <el-input v-model="addList.roomTypeId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型ID"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="房间类型名称" prop="roomTypeName">
          <el-input v-model="addList.roomTypeName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型名称"/>
        </el-form-item>
        <el-form-item label="房间价格" prop="roomPrice">
          <el-input v-model="addList.roomPrice" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入房间价格"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="addRoomType('ruleForm')">确 定</el-button>
    <el-button @click="closeRoomType('ruleForm')">取 消</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="修改"
      :visible.sync="updateButtonController"
      width="30%"
      @close="closeRoomType('ruleForm')"
    >
      <el-form label-width="20%" :model="updateList" ref="ruleForm">
        <!--        <el-form-item label="房间类型表ID" prop="roomTypeTableId">-->
        <!--          <el-input v-model="updateList.roomTypeTableId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型表ID"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="房间类型ID" prop="roomTypeId">-->
        <!--          <el-input v-model="updateList.roomTypeId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型名称"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="房间类型名称" prop="roomTypeName">
          <el-input v-model="updateList.roomTypeName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型名称"/>
        </el-form-item>
        <el-form-item label="房间价格" prop="roomPrice">
          <el-input v-model="updateList.roomPrice" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入房间价格"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="updateRoomType('ruleForm')">确 定</el-button>
    <el-button @click="closeRoomType('ruleForm')">取 消</el-button>
  </span>
    </el-dialog>

    <!--删除弹窗-->
    <el-dialog
      title="删除"
      width="30%"
      :visible.sync="deleteButtonController"
      @close="closeDelete">

      <div style="margin: 10px"><span>请确认是否删除</span></div>

      <span slot="footer" class="dialog-footer">
          <el-button  type="primary" @click="deleteRoomType('ruleForm')">确定</el-button>
          <el-button  @click="closeDelete('ruleForm')">取消</el-button>
        </span>
    </el-dialog>

    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:10% ;" @click="addButton">
      新增
    </el-button>

    <el-input
      v-model="roomPrice"
      placeholder="房间价格"
      name="roomPrice"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />

    <el-input
      v-model="roomTypeName"
      placeholder="房间类型名称"
      name="roomTypeName"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />

    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:40px ;" @click="selectRoomType">查询</el-button>
    <el-button  style="width:80px;margin-bottom:30px;margin-left:40px ;" @click="checkRoomType">重置</el-button>
    <el-table :data="roomTypeList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="659px" border style="width: 100%;margin-top: 20px">
      <!--      <el-table-column align="center" fixed prop="roomTypeId" label="房间类型ID"/>-->
      <el-table-column align="center" fixed prop="roomTypeName" label="房间类型名称"/>
      <el-table-column align="center" fixed prop="roomPrice" label="房间价格"/>
      <el-table-column align="center" fixed prop="remark" label="备注"/>
      <el-table-column align="center" fixed label="操作">
        <template v-slot="scope">
          <el-button size="mini" round icon="el-icon-edit" @click="updateButton(scope.row)"></el-button>
          <el-button size="mini" round icon="el-icon-delete" @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>

    </el-table>

    <div style="margin-top:20px; text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="roomTypeList.length">
      </el-pagination>
    </div>


  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {deleteUser, insertUser, selectAllUser} from "@/api/user";
import {deleteRoomType, insertRoomType, selectAllRoomType, updateRoomType} from "@/api/roomType";

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data(){
    return{
      currentPage: 1,
      currentIndex: '',
      pagesize: 10,

      roomPrice:'',
      roomTypeName:'',
      roomTypeList: [],
      addButtonController:false,
      updateButtonController:false,
      deleteButtonController:false,
      deleteRoomTypeId:'',
      // addOrUpdateList:{
      //   roomTypeTableId:'',
      //   roomTypeId:'',
      //   roomTypeName:'',
      //   roomPrice:'',
      //   remark:''
      // },
      addList:{
        // roomTypeTableId:'',
        // roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      },
      updateList:{
        // roomTypeTableId:'',
        // roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      },
    }
  },
  mounted() {
    this.selectRoomType()
  },
  methods:{
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage= val
    },

    selectRoomType(){
      this.currentPage = 1
      selectAllRoomType({
        roomPrice:this.roomPrice,
        roomTypeName:this.roomTypeName
      }).then(
        response => {
          console.log(response.data)
          this.roomTypeList = response.data
        }
      )
    },
    addButton(){
      this.addList = {
        // roomTypeTableId:'',
        // roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      }
      this.addButtonController = true
    },
    updateButton(row){
      this.updateList = {
        roomTypeTableId:row.roomTypeTableId,
        roomTypeId:row.roomTypeId,
        roomTypeName:row.roomTypeName,
        roomPrice:row.roomPrice,
        remark:row.remark
      }
      this.updateButtonController = true
    },
    deleteButton(row){
      this.deleteRoomTypeId = row.roomTypeId
      this.deleteButtonController = true
    },

    addRoomType(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertRoomType({
            // roomTypeTableId:this.addList.roomTypeTableId,
            // roomTypeId:this.addList.roomTypeId,
            roomTypeName:this.addList.roomTypeName,
            roomPrice:this.addList.roomPrice,
            remark:this.addList.remark
          }).then(
            response => {
              this.addButtonController = false
              this.$message.success("新增成功！"); // 添加成功提示
              this.selectRoomType()
            }
          )

        } else {return false}
      })
    },

    updateRoomType(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateRoomType({
            roomTypeTableId:this.updateList.roomTypeTableId,
            roomTypeId:this.updateList.roomTypeId,
            roomTypeName:this.updateList.roomTypeName,
            roomPrice:this.updateList.roomPrice,
            remark:this.updateList.remark
          }).then(
            response => {
              this.updateButtonController = false
              this.selectRoomType()
            }
          )

        } else {return false}
      })
    },

    deleteRoomType(formName) {
      deleteRoomType({
        roomTypeId:this.deleteRoomTypeId
      }).then(
        response => {
          this.deleteButtonController = false
          this.selectRoomType()
        }
      )
    },
    closeDelete(){
      this.addButtonController = false
      this.updateButtonController = false
      this.deleteButtonController = false
    },
    checkRoomType(){
      this.roomPrice = ''
      this.roomTypeName = ''
      this.selectRoomType()
    },
    closeRoomType(formName){
      this.$refs[formName].resetFields();
      this.addButtonController = false;
      this.updateButtonController = false;
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
